#  -*- coding:utf-8 -*- 
"""
@ author: 罗金盛
@ time: 2024/3/6 
@ file: 15.多进程.py

"""

import multiprocessing
import time

def num(name):
    print('run process {}'.format(name))
    time.sleep(3)
    print('run {} end'.format(name))

if __name__ == '__main__':
    # 进程池 在一开始就创建好一个袋子 N个进程 哪个空闲就拿出来直接用
    pool = multiprocessing.Pool(3)
    for i in range(10):
        # pool.apply(func=num,args=(i,))    # apply 同步
        pool.apply_async(func=num,args=(i,))    # apply_async 异步

    pool.close()
    pool.join()


    # processes = []
    # for i in range(5):
    #     p = multiprocessing.Process(target=num,args=(i,))
    #     processes.append(p)
    #     p.start()
    #
    # for p in processes:
    #     p.join()

    print('all done')














